$(document).ready(init);

function init() {
	showContent({
		shenhe : 'shenhe',
		appType : 0,
		useType : 1
	});
	
	$('#search').click(doSearch);
}

function doSearch() {
	showContent({
		shenhe : 'shenhe',
		appType : 0,
		useType : 1,
		startTime : $.trim($('#startTime').val()),
		endTime : $.trim($('#endTime').val())
	});
	
}


var remark= '审批通过';
/**
 * 显示信息
 */
function showContent(queryParams) {
	$('#appointments').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'appointment-admin!queryAppointmentsInPage.action',
	    idField: 'empID',
	    queryParams :queryParams,
	    columns:[[
	        {field:'appName',title:'仪器名称',width:fixWidth(0.08)},
	        {field:'empName',title:'预约人',width:fixWidth(0.08)},
	        {field:'loginAccount',title:'登陆账号',width:fixWidth(0.1)},
	        {field:'phone',title:'联系方式',width:fixWidth(0.1)},
	        {field:'startTime',title:'预约开始时间',width:fixWidth(0.1)},
	        {field:'endTime', title:'预约结束时间',width:fixWidth(0.1)},
	        {field:'remark', title:'备注', width:fixWidth(0.2)},
	        {field:'id',title:'审核',width:fixWidth(0.1),align:'center',
	        	    formatter : function(value, rowData, rowIndex) {
						if (rowData.editing) {
							return '<label style="color:red">' + remark + '</label>';
				 		} else {
				 			 var e = '<a href="#" onclick="agree('+ rowIndex + ',\'' + value +'\')">同意&nbsp;&nbsp;</a> '
				 			 var f = '<a href="#" onclick="refuse('+ rowIndex + ',\'' + value +'\')">拒绝&nbsp;&nbsp;</a> ';
							return e + f;
						}
					}
			}
	    ]],
	    onBeforeEdit:function(rowIndex,rowData){
			rowData.editing = true;
			$('#appointments').datagrid('refreshRow', rowIndex);
		},
		onAfterEdit:function(rowIndex,rowData){
			rowData.editing = true;
			$('#appointments').datagrid('refreshRow', rowIndex);
		},
	    pagination:true,
	    rownumbers:true,
	    fitColumns:true
	});
}

function agree(index, value) {
	review(index, value, '您确定审核<b>通过</b>？', 'agree'); 
}

function refuse(index, value) {
	review(index, value, '您确定审核<b>拒绝</b> ？', 'refuse'); 
}

function review(index, value, title, taidu) 
{
	remark= '审批通过';
	
	 $.messager.confirm('审核', title, function(r){
        if (r){
           var params = {
				'taidu' : taidu,
				id : value
			}
			
			var option = {
		   		type: 'POST',
		   		url: 'appointment-admin!reviewAppointments.action',
		   		data: params,
		   		success : function(msg){
		   			if (msg == 'success') {
		   				if (taidu == 'refuse') {
		   					remark = '审批拒绝';
		   				}
		   				
			   			$.messager.alert('确定', '审批完成', 'info')
				     	
				     	$('#appointments').datagrid('beginEdit', index);
		   			} else {
		   				$.messager.alert('确定', '审批失败', 'info')
		   			}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert(' ', '处理失败：' + errorThrown, 'error')
				}
			}
			
			$.ajax(option); 
        } else {
        	$('#appointments').datagrid('cancelEdit', index);
        }
     });
}